/*
 * Copyright 2024, AutoMQ HK Limited.
 *
 * The use of this file is governed by the Business Source License,
 * as detailed in the file "/LICENSE.S3Stream" included in this repository.
 *
 * As of the Change Date specified in that file, in accordance with
 * the Business Source License, use of this software will be governed
 * by the Apache License, Version 2.0
 */

package com.automq.stream.s3;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import java.util.Random;
import java.util.UUID;

public class TestUtils {

    public static ByteBuf random(int size) {
        byte[] bytes = new byte[size];
        new Random().nextBytes(bytes);
        // In the most test cases, the generated ByteBuf will be released after write to S3.
        // To give the ByteBuf a chance to assert in the unit tests, we just retain it here.
        // Since the retained ByteBuf is unpooled, it will be released by the GC.
        return Unpooled.wrappedBuffer(bytes).retain();
    }

    public static ByteBuf randomPooled(int size) {
        byte[] bytes = new byte[size];
        new Random().nextBytes(bytes);
        ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(size);
        buf.writeBytes(bytes);
        return buf;
    }

    public static String tempFilePath() {
        return System.getProperty("java.io.tmpdir") + "/kos-" + UUID.randomUUID();
    }
}
